package com.shujia.window

import org.apache.flink.streaming.api.scala._

object DEmo3CountWindow {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    /**
      * 统计最近15秒单词的数量每隔5秒统计一次
      *
      */

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    val kvDS: DataStream[(String, Int)] = linesDS.flatMap(_.split(",")).map((_, 1))

    /**
      * 统计窗口，多少条数据计算一次
      * countWindow(10): 滚动的统计窗口
      * countWindow(10,2): 滑动的统计窗口
      */

    kvDS
      .keyBy(_._1)
      .countWindow(10, 2)
      .sum(1)
      .print()


    env.execute()
  }

}
